1. Field of the Invention
This invention relates to user interfaces for computer systems. More particularly, this invention relates to matching a client-supplied string to requirements of a software component of a computer system.
2. Description of the Related Art
In user interfaces for computer systems, software components often contain predefined text strings that can be selected by clients. For example, a calendar interface may provide several predefined strings that can be used to specify month names. Clients provide one of these predefined strings when calling the relevant methods of the calendar object. Internally, the calendar implementation must determine which, if any, of the predefined strings is matched by the client-supplied string.
Conventionally, the algorithm for matching the client-supplied string is often coded as shown in the following pseudocode fragment:
if ClientString equals “JANUARY” then use monthNum=1
else if ClientString equals “FEBRUARY” then use monthNum=2
else if ClientString equals “MARCH” then use monthNum=3
else if ClientString equals “APRIL” then use monthNum=4
This is not an efficient implementation for a number of reasons: The operation of comparing two strings for equality is computationally expensive. It may involve a call to the underlying operating system to compare the memory ranges of the two strings for equality. Calls to the underlying operating system typically involve a context switch, which is an expensive operation. Furthermore, matching strings by performing a complete content comparison as described above ignores other, more readily available information.
One approach to matching strings more efficiently involves pre-placement of candidate strings into a hash table at runtime, before the desired lookup is performed. A lookup of a client-supplied string in the hash table according to its hash value is then performed. This approach incurs a runtime performance cost to initialize the hashtable and a memory cost to store the hashtable.
Lexical analyzers, such as the program lex in the Unix environment have long been available for matching strings. Lex matches the characters of strings sequentially. Sequential matching cannot be implemented efficiently in all programming environments. For example, in some versions of Java 2 Micro Edition (J2ME), access to individual characters of a string requires one method call per character, which is inefficient. A need remains for a more efficient technique for rejecting invalid data during string matching.